<template>
  <div>
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="用户名" prop="adminName">
        <el-input v-model="formData.adminName" placeholder="请输入用户名" :maxlength="11" show-word-limit clearable
                  prefix-icon='el-icon-mobile' :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" placeholder="请输入密码" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入邮箱" clearable :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="telephone">
        <el-input v-model="formData.telephone" placeholder="请输入电话" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label="是否可用" prop="state" required>
        <el-switch v-model="formData.state" :active-value='1' :inactive-value='0'></el-switch>
      </el-form-item>
      <el-form-item size="large">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'userForm',
  components: {},
  props: [],
  data() {
    return {
      formData: {
        adminName: '',
        password: undefined,
        email: undefined,
        telephone: undefined,
        state: 1
      },
      rules: {
        adminName: [{
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }],
        email: [],
        telephone: []
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/api/admin', this.formData)
        if (res.code !== 0) return this.$message.error(res.msg)
        this.$message.success('新增用户成功')
        this.resetForm()
        this.$emit('msg', false)
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    }
  }
}

</script>
<style>
</style>
